//
//  KSChooseTimeView.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/9/22.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

protocol KSCompleteEventDelegate: NSObjectProtocol {
    func completeClick(startTime: String,endTime: String)
    func cancleClick()
}

enum chooseTimeType {
    case StartTime
    case EndTime
}

class KSChooseTimeView: UIView {
    
    weak var delegate: KSCompleteEventDelegate?
    var startTime = String()
    var endTime = String()
    private var timeType = chooseTimeType.StartTime
    private let timeLabel = UILabel()
    private let startBtn = UIButton()
    private let endBtn = UIButton()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        let date = Date()
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyy-MM-dd"
        
        startTime = formatter.string(from: date)
        endTime = formatter.string(from: date)
        
        let headView = UIView()
        headView.backgroundColor = UIColor.white
        self.addSubview(headView)
        headView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.centerY).offset(-64/2)
            make.left.right.equalTo(0)
            make.height.equalTo(50)
        }
        
        let headLineView = UIView()
        headLineView.backgroundColor = KSColor.kColor_line
        headView.addSubview(headLineView)
        headLineView.snp.makeConstraints { (make) in
            make.bottom.equalTo(headView.snp.bottom).offset(0)
            make.left.right.equalTo(0)
            make.height.equalTo(1)
        }
        
        let completeBtn = UIButton()
        completeBtn.setTitle("完成", for: .normal)
        completeBtn.addTarget(self, action: #selector(completeEvent), for: .touchDown)
        completeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        completeBtn.setTitleColor(KSColor.kColor_3, for: .normal)
        headView.addSubview(completeBtn)
        completeBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-15)
        }
        
        self.setTimeString()
        headView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        let cancelBtn = UIButton()
        cancelBtn.setBackgroundImage(UIImage.init(named: "quxiao"), for: .normal)
        cancelBtn.addTarget(self, action: #selector(cancelEvent), for: .touchDown)
        headView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
            make.width.height.equalTo(17)
        }
        
        let bottomView = UIView()
        self.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(self.snp.bottom).offset(0)
            make.height.equalTo(50)
        }
        
        let bottomLineView = UIView()
        bottomLineView.backgroundColor = KSColor.kColor_line
        bottomView.addSubview(bottomLineView)
        bottomLineView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.height.equalTo(1)
        }
        
        startBtn.setTitle("开始日期", for: .normal)
        startBtn.setTitleColor(KSColor.kColor_3, for: .normal)
        startBtn.backgroundColor = UIColor.colorWithHexString(hex: "#F4F5F6")
        startBtn.addTarget(self, action: #selector(chooseStartTime), for: .touchDown)
        bottomView.addSubview(startBtn)
        startBtn.snp.makeConstraints { (make) in
            make.top.equalTo(bottomLineView.snp.bottom).offset(0)
            make.left.equalTo(0)
            make.bottom.equalTo(bottomView.snp.bottom)
            make.right.equalTo(bottomView.snp.centerX)
        }
        
        endBtn.setTitle("结束日期", for: .normal)
        endBtn.backgroundColor = UIColor.white
        endBtn.setTitleColor(KSColor.kColor_3, for: .normal)
        endBtn.addTarget(self, action: #selector(chooseEndTime), for: .touchDown)
        bottomView.addSubview(endBtn)
        endBtn.snp.makeConstraints { (make) in
            make.top.equalTo(bottomLineView.snp.bottom).offset(0)
            make.right.equalTo(0)
            make.bottom.equalTo(bottomView.snp.bottom)
            make.left.equalTo(bottomView.snp.centerX)
        }
        
        let datePicker = UIDatePicker.init()
        datePicker.locale = Locale.init(identifier: "zh_CN")
        datePicker.datePickerMode = .date
        datePicker.backgroundColor = UIColor.white
        datePicker.addTarget(self, action: #selector(chooseTime(picker:)), for: .valueChanged)
        self.addSubview(datePicker)
        datePicker.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(headView.snp.bottom).offset(0)
            make.bottom.equalTo(bottomView.snp.top).offset(0)
        }
    }
    
    @objc private func completeEvent() {
        self.delegate?.completeClick(startTime: self.startTime, endTime: self.endTime)
        self.isHidden = true
    }
    
    @objc private func cancelEvent() {
        self.delegate?.cancleClick()
        self.isHidden = true
    }
    
    @objc private func chooseStartTime() {
        timeType = chooseTimeType.StartTime
        self.setBtnBackColor()
        startBtn.backgroundColor = UIColor.colorWithHexString(hex: "#F4F5F6")
        self.setTimeString()
    }
    
    @objc private func chooseEndTime() {
        timeType = chooseTimeType.EndTime
        self.setBtnBackColor()
        endBtn.backgroundColor = UIColor.colorWithHexString(hex: "#F4F5F6")
        self.setTimeString()
    }
    
    private func setBtnBackColor() {
        startBtn.backgroundColor = UIColor.white
        endBtn.backgroundColor = UIColor.white
    }
    
    @objc private func chooseTime(picker: UIDatePicker) {
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyy-MM-dd"
        let timeString = formatter.string(from: picker.date)
        switch timeType {
        case .StartTime: startTime = timeString
        case .EndTime: endTime = timeString
        }
        self.setTimeString()
    }
    
    private func setTimeString() {
        var startColor = KSColor.kColor_3
        var endColor = KSColor.kColor_3
        switch timeType {
        case .StartTime: startColor = KSColor.kColorMainRed
        case .EndTime: endColor = KSColor.kColorMainRed
        }
        
        let attributed = NSMutableAttributedString()
        let start = NSAttributedString.init(string: self.startTime, attributes: [.foregroundColor : startColor,.font : UIFont.systemFont(ofSize: 17.0)])
        let word = NSAttributedString.init(string: "至", attributes: [.foregroundColor : KSColor.kColor_3,.font : UIFont.systemFont(ofSize: 17.0)])
        let end = NSAttributedString.init(string: self.endTime, attributes: [.foregroundColor : endColor,.font : UIFont.systemFont(ofSize: 17.0)])
        attributed.append(start)
        attributed.append(word)
        attributed.append(end)
        self.timeLabel.attributedText = attributed
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
